﻿using System.Windows;
using System.Windows.Controls;
using Multitouch.Framework.WPF.Input;
using Project_CFT2.View;
using Project_CFT2.ViewModel;

namespace Project_CFT2.Factory
{
    class TouchablePanelRssViewFactory : IPanelViewFactory
    {
        public Panel Panel { get; set; }

        public void CreateViewFromDataContext2(object dataContext, Point position, Panel p)
        {
        }

        public void CreateViewFromDataContext(object dataContext, Point position)
        {
            var viewModel = dataContext as ViewModelBase;

            if (viewModel == null) return;

            FrameworkElement newView;

            if (viewModel is FurnitureViewModel)
            {

                newView = new Trash();
                newView.SetValue(Canvas.LeftProperty, position.X);
                newView.SetValue(Canvas.TopProperty, position.Y);
                MultitouchScreen.AddContactEnterHandler(newView, ElementCaptureHandler);
                Panel.Children.Add(newView);
                newView = new FeedItemView();
            }
            else
            {
                newView = new FolderAndFeedView();

                newView.DataContext = viewModel;
                newView.SetValue(Canvas.LeftProperty, position.X);
                newView.SetValue(Canvas.TopProperty, position.Y);
                MultitouchScreen.AddContactEnterHandler(newView, ElementCaptureHandler);
                Panel.Children.Add(newView);

            }
        }

        private static void ElementCaptureHandler(object sender, ContactEventArgs e)
        {
            var element = sender as IInputElement;
            if (element != null)
                e.Contact.Capture(element);
        }

    }
}
